package com.eningqu.aipen.ui.medical;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.eningqu.aipen.demo.databinding.FragmentMedicalBinding;
import com.eningqu.aipen.logic.network.Apis;
import com.eningqu.aipen.logic.network.BaseRetrofitUtils;
import com.eningqu.aipen.logic.network.RxLifecycle;
import com.eningqu.aipen.logic.network.SchedulerTransformer;
import com.eningqu.aipen.ui.adapter.MedicalAdapter;
import com.eningqu.aipen.ui.base.BaseFragment;
import com.eningqu.aipen.ui.dialog.LoadingAlertDialog;
import com.eningqu.aipen.utils.GlobalConfig;
import com.eningqu.aipen.utils.SPUtil;
import com.kongzue.dialogx.dialogs.PopTip;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;

/**
 * Package:com.eningqu.aipen.ui.medical
 * Author:starr
 * Time:2023/11/7 14:51
 * Description: 病历本滑动列表界面，展示所有的病历本
 */
public class MedicalFragment extends BaseFragment<FragmentMedicalBinding> {

    private final ArrayList list = new ArrayList();
    private MedicalAdapter adapter;

    private LoadingAlertDialog loadingDialog;
    RecyclerView recyclerView;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        loadingDialog = new LoadingAlertDialog(getContext());
        recyclerView = mBinding.rvMedical;
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, true));


        SmartRefreshLayout refreshLayout1 = mBinding.smartRefresh;
        refreshLayout1.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh(2000);
            async();
        });
    }

    @Override
    public void onStart() {
        super.onStart();
    }


    public static Fragment newInstance() {
        return new MedicalFragment();
    }

    @SuppressLint({"CheckResult", "NotifyDataSetChanged", "SetTextI18n"})
    @Override
    protected void async() {
        loadingDialog.show();
        BaseRetrofitUtils.getInstance().create(Apis.class).findAllMedicalBook(1)
                .compose(new SchedulerTransformer<>())
                .compose(RxLifecycle.bindExLifecycle(this))
                .subscribe(bean -> {
                    loadingDialog.dismiss();
                    if (bean != null) {
                        list.clear();
                        list.addAll(bean.page);
                        mBinding.tvMy.setText("我的患者(" + list.size() + ")");
                        adapter.notifyDataSetChanged();
                        adapter.notifyItemInserted(list.size() - 1);
                        PopTip.tip("数据获取成功");
                    }
                });

        adapter = new MedicalAdapter(list);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initStatusBar() {
        mBinding.titleTopBar.baseTvTitle.setText("病历本");
    }


    public static class MedicalClick {

    }
}
